package com.example.core;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public abstract class WebItemsCrawler<T> extends WebInfoCrawler<List<T>> {
    private final String itemsXpath;

    public WebItemsCrawler(String url, String itemsXpath) throws IOException {
        super(getUrl(url));
        this.itemsXpath = itemsXpath;
    }

    protected List<T> parse() throws Exception {
        Elements elements = doc.selectXpath(itemsXpath);
        List<T> result = new ArrayList<>();
        for (Element element : elements) {
            T row = this.parseItem(element);
            if (row != null) {
                result.add(row);
            }
        }
        return result;
    }

    protected abstract T parseItem(Element element) throws Exception;
}
